package il.ac.braude.sona.ocsf.server;

import il.ac.braude.sona.entities.Messege;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
/**
 * a class to expose messege api 
 * @author <p>SONA, Sasha,Ofek,Nitzan and Avihai, Group 6,<br>Laboratory in Software Engineering,Ort Braude College.</p>
 *
 */
public class MessegesAPI {

	/**
	 * hash which types should get which mssgs
	 */
	static HashMap<String,String> typesToMssg;
	/**
	 * initializing the hash
	 */
	static {
		typesToMssg = new HashMap<String,String>();
		Connection con = null;
		try {
			con = DatabaseController.getConnection();
			ResultSet res = con.prepareStatement("select * from mssgs_types").executeQuery();
			while (res.next()) 
				typesToMssg.put(res.getString("mtid"),res.getString("mssg_title"));
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (con!=null)
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
			
		
	}
	/**
	 * helper for sending extensions
	 * @param uid
	 * @param rid
	 */
	public static void sendExtensionApprovedMessege(String uid,String rid) {
		sendExtensionDecisionMessege(uid, rid,"APPROVE");
	}
	/**
	 * helper for sending extension messeges
	 * @param uid
	 * @param rid
	 * @param eid
	 */
	public static void sendExtensionMessege(String uid,String rid,String eid) {
		sendMessege(uid,rid,getEidWhereRid(rid),"EXTENSION");
	}
	/**
	 * helper for sending extension decisions messeges
	 * @param uid
	 * @param rid
	 * @param decision
	 */
	public static void sendExtensionDecisionMessege(String uid,String rid,String decision) {
		sendMessege(uid,rid,getEidWhereRid(rid),"EXTENSION_"+decision);
	}
	/**
	 *  helper for sending exceptions
	 * @param uid
	 * @param rid
	 * @param eid
	 */
	public static void sendExceptionMessege(String uid,String rid,String eid) {
		sendMessege(uid,rid,eid,"EXCEPTION");
	}
	/**
	 * helper for sending mssgs of some type
	 * @param uid
	 * @param rid
	 * @param eid
	 * @param type
	 */
	public static void sendMessege(String uid,String rid,String eid,String type) {
		Connection con = null;
		try {
			con = DatabaseController.getConnection();
			if (eid!=null) SQLHelper.insert(con, "messeges", new String[] { "uid","mtype","rid","eid" }, new String[] { uid,type, rid , eid });
			else SQLHelper.insert(con, "messeges", new String[] { "uid","mtype","rid" }, new String[] { uid,type, rid });
		} catch (SQLException e) {
		} finally {
			if (con!=null)
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	}
	/**
	 * helper for sending mssgs of some type
	 * @param con
	 * @param uid
	 * @param rid
	 * @param eid
	 * @param type
	 */
	public static void sendMessege(Connection con,String uid,String rid,String eid,String type){
		try {SQLHelper.insert(con, "messeges", new String[] { "uid","mtype","rid","eid" }, new String[] { uid,type, rid , eid });}catch(SQLException e){}	
	}
	/**
	 * helper for sending reminders
	 * @param uid
	 * @param rid
	 * @param eid
	 */
	public static void sendReminderMessege(String uid,String rid,String eid) {
		sendMessege(uid,rid,eid,"REMINDER");
	}
	/**
	 * helper for notice
	 * @param uid
	 * @param rid
	 * @param eid
	 */
	public static void sendNoticeMessege(String uid,String rid,String eid) {
		sendMessege(uid,rid,eid,"NOTICE");
	}
	/**
	 * helper for suspend
	 * @param uid
	 * @param rid
	 */
	public static void sendSuspendedMessege(String uid,String rid) {
		sendMessege(uid,rid,getEidWhereRid(rid),"SUSPENDED");
	}
	/**
	 * helper for getting all messeges
	 * @param id
	 * @return mssgs
	 */
	public static ArrayList<Messege> getAllMessges(String id) {
		ArrayList<Messege> mssgs = new ArrayList<Messege>();
		Connection con = null;
		try {
			con = DatabaseController.getConnection();
			ArrayList<HashMap<String, String>> res = SQLHelper.select( "messeges", "uid='"+id+"'",con, "mtype" , "rid" , "eid" , "uid");
			for (HashMap<String, String> hash : res) {
				mssgs.add(new Messege(hash.get("rid"),hash.get("eid"),typesToMssg.get(hash.get("mtype")),hash.get("uid")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (con!=null)
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		return mssgs;
	}
	/**
	 * helper for sending reminders
	 * @param con
	 * @param eid
	 * @param rid
	 * @param uid
	 * @throws SQLException
	 */
	public static void sendReminderMessege(Connection con,String eid,String rid,String uid) throws SQLException {
		sendMessege(con,uid, rid, eid, "REMINDER");
		
	}
	/**
	 * helper for getting eid given a specific rid
	 * @param rid
	 * @return String
	 */
	private static String getEidWhereRid(String rid){
		ArrayList<String> eids=new ArrayList<String>();
		Connection con=null;
    	try {
    		con  = DatabaseController.getConnection();
    		SQLHelper.use(con, DatabaseController.dbName);
    		ArrayList<HashMap<String, String>> results = SQLHelper.select("status_execution","rid='"+rid+"' and date_finished is NULL",con,"eid");
    		if (results.size()>0)
    			eids.add(results.get(0).get("eid"));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (con!=null)
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
    	return eids.get(0);
	}

}
